/*
 * ksCatalogAtomBlockReader.cpp
 *
 *  Created on: Aug 3, 2011
 *      Author: wang
 */

#include "ksCatalogAtomBlockReader.h"
#include <cstring>
#include <fstream>
#include <sstream>

#include "../ksDefines.h"
#include "../ksData.h"
#include <sstream>
#include "../writer/ksCatalogAtomBlockWriter.h"
#include "../ksException.h"

ksCatalogAtomBlockReader::ksCatalogAtomBlockReader()
{

}

ksCatalogAtomBlockReader::~ksCatalogAtomBlockReader()
{

}

void ksCatalogAtomBlockReader::extract(const char *path)
{
    std::stringstream stringStream;
    stringStream << _blockHeader.atom.id;
    std::string filename = stringStream.str();

    std::string fullpath = std::string(path) + filename;
    std::ofstream output;
    output.open(fullpath.c_str(), std::ofstream::out | std::ofstream::binary);
    if (output.good())
    {
        std::istringstream in(std::string((char*) _data, _blockHeader.atom.dataLength));
        while (in.good() && !in.eof())
        {
            std::cout << "read at:" << in.tellg();
            unsigned char type = 0;
            in.read((char*) &type, sizeof(unsigned char));
            std::cout << " type end:" << in.tellg();
            if (type == ksCatalogEntry::EVolume)
            {
                // read title length
                unsigned char length = 0;
                in.read((char*) &length, sizeof(unsigned char));
                std::cout << " title length end:" << in.tellg();
                if (length == 0)
                {
                    return;
                }
                // read title content
                char buffer[255] = { 0 };
                in.read(buffer, length);
                std::cout << " title content end:" << in.tellg() << std::endl;

                // write to output
                output << "volume:{title:";
                output.write(buffer, length);
                output << "}" << std::endl;
            }
            else if (type == ksCatalogEntry::EChapter)
            {
                // read offset;
                unsigned int offset = 0;
                in.read((char*) &offset, sizeof(unsigned int));
                std::cout << " offset end:" << in.tellg();

                // read title length
                unsigned char length = 0;
                in.read((char*) &length, sizeof(unsigned char));
                std::cout << " title length end:" << in.tellg();

                if (length == 0)
                {
                    return;
                }

                // read title content
                char buffer[255] = { 0 };
                in.read(buffer, length);
                std::cout << " title content end:" << in.tellg() << std::endl;

                // write to output
                output << "chapter:{title:";
                output.write(buffer, length);
                output << ", offset:";
                output << offset;
                output << "}" << std::endl;
            }
            else
            {
                throw ksException("not excepted catalog type");
            }
        }
    }
    else
    {
        std::cout << "write file filed:" << fullpath << std::endl;
    }

    output.close();
}

